package com.iot.service;

import com.iot.core.JsonHelper;
import com.iot.mapper.*;
import com.iot.pojo.*;
import com.iot.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class Tline_emp_setServiceImpl implements Tline_emp_setService {

    @Autowired
    private Tline_emp_setMapper mapper;
    @Autowired
    private DTline_emp_setMapper dmapper;
    @Autowired
    private DTreeMapper dTreeMapper;
    @Autowired
    private DRefMapper dRefMapper;
    @Autowired
    private Tdevice_emp_setMapper devicemapper;
    @Autowired
    private TdeviceMapper tdeviceMapper;

    @Autowired
    private Twarn_recordMapper twarn_recordMapper;
    @Autowired
    private Twarn_typeMapper twarn_typeMapper;
    @Autowired
    private TempMapper tempMapper;

    @Override
    public int getRecordCount(String sWhere) {
        return mapper.getRecordCount(sWhere);
    }

    @Override
    public int insert(MTline_emp_set mTline_emp_set) throws Exception {
        int count = 0;
        count = getRecordCount(" AND BDEL=1 AND LINE_ID='" + mTline_emp_set.getLINE_ID() + "' AND EMP_ID=" + mTline_emp_set.getEMP_ID() + "");
        if (count > 0)
            throw new Exception("该设置已存在");
        return mapper.insert(mTline_emp_set);
    }


    @Override
    public void update(MTline_emp_set mTline_emp_set) throws Exception {
        int count = 0;
        count = getRecordCount(" AND BDEL=1 AND LINE_ID='" + mTline_emp_set.getLINE_ID() + "' AND EMP_ID=" + mTline_emp_set.getEMP_ID() + " AND LINE_EMP_SET_ID!=" + mTline_emp_set.getLINE_EMP_SET_ID() + "");
        if (count > 0)
            throw new Exception("该设置已存在");
        mapper.update(mTline_emp_set);
    }

    @Override
    public void deleteById(int id) {

        MTline_emp_set entity = mapper.single(id);

        int empId = entity.getEMP_ID();
        List<MTdevice_emp_set> list = devicemapper.fetch("AND EMP_ID=" + empId);

        for (MTdevice_emp_set model : list
        ) {
            model.setBDEL(0);
            devicemapper.update(model);
        }

        entity.setBDEL(0);
        mapper.update(entity);
    }

    @Override
    public MTline_emp_set single(Integer id) {
        return mapper.single(id);
    }

    @Override
    public List<MTline_emp_set> fetch(String sWhere) {
        return mapper.fetch(sWhere);
    }

    @Override
    public List<Map<String, Object>> fetchByPage(Map<String, Object> map) {
        return dmapper.fetchByPage(map);
    }


    @Override
    public List<Map<String, Object>> getDeviceByEmpId(Map<String, Object> map) {

        List<Map<String, Object>> tempMap=dmapper.getDeviceByEmpId(map);
        tempMap=getDoorStatus(tempMap);
        return tempMap;
    }

    private List<Map<String, Object>> getDoorStatus(List<Map<String, Object>> deviceMap) {
        for (Map<String, Object> tempMap :
                deviceMap) {
            tempMap.put("DOORDESC", "");
            tempMap.put("DOORSTATUS", "");
            tempMap.put("BATTERYLEVEL", "");
            tempMap.put("EMP_IDNAME", "");
            tempMap.put("WARNSTATUS", "");
            tempMap.put("WARNSDESC", "");
            tempMap.put("LOCKSTATUS", "");
            tempMap.put("LOCKDESC", "");
            tempMap.put("SENSORDATE", "");
            tempMap.put("SOAKING", "");
            tempMap.put("TEMPERATURE", "");
            tempMap.put("HUMIDITY", "");
            tempMap.put("QUAKE", "");
            tempMap.put("TILT", "");
            int deviceId = Common.toInt(tempMap.get("DEVICE_ID"));
            MTdevice md = tdeviceMapper.single(deviceId);
            MTwarn_record mTwarn_record = twarn_recordMapper.singleByWhere("AND DEVICE_ID=" + deviceId + "  ORDER BY SDATE DESC ");
            if (mTwarn_record != null) {
                MTwarn_type mTwarnType = twarn_typeMapper.single(mTwarn_record.getWARN_TYPE_ID());
                if (mTwarnType != null) {
                    if (mTwarnType.getMEANCODE() > 1) {
                        tempMap.put("WARNSTATUS", 1);
                        tempMap.put("WARNSDESC", mTwarnType.getSNAME());
                    } else {
                        tempMap.put("WARNSTATUS", 0);
                    }

                }
                tempMap.put("SENSORDATE", mTwarn_record.getSDATE());
                if (md.getTYPE_ID() == 1) {
                    MTnb_dev_data data = JsonHelper.toBean(mTwarn_record.getJSONSTR(), MTnb_dev_data.class);
                    MTemp mTemp = tempMapper.single(mTwarn_record.getEMP_ID());
                    String doors = YT.decimalToBinary(data.getDOORLOCK());
                    int mdoors = md.getDOOR_NUM();
                    String descDoor = " ";
                    for (int i = 0; i < mdoors; i++) {
                        char ch = doors.charAt(i);
                        if (ch == '1') {
                            descDoor += (i + 1) + "号门被打开 ";
                        } else {
                            descDoor += (i + 1) + "号门关闭 ";
                        }
                    }

                    if (Common.toInt(data.getDOORLOCK()) > 0) {
                        tempMap.put("DOORSTATUS", 1);
                    } else {
                        tempMap.put("DOORSTATUS", 0);
                    }

                    tempMap.put("DOORDESC", descDoor.trim());
                    //电量
                    tempMap.put("BATTERYLEVEL", data.getBATTERYLEVEL());
                    //进水
                    tempMap.put("SOAKING", data.getSOAKING());
                    //温度
                    tempMap.put("TEMPERATURE", data.getTEMPERATURE());
                    //湿度
                    tempMap.put("HUMIDITY", data.getHUMIDITY());
                    //震动
                    tempMap.put("QUAKE", data.getQUAKE());
                    //倾斜
                    tempMap.put("TILT", data.getTILT());
                    //电磁锁
                    if (Common.toInt(data.getLOCKSTATUS()) > 0) {
                        tempMap.put("LOCKSTATUS", 1);
                    } else {
                        tempMap.put("LOCKSTATUS", 0);
                    }
                    String descLock = " ";
                    String lock = YT.decimalToBinary(data.getLOCKSTATUS());
                    for (int i = 0; i < mdoors; i++) {
                        char ch = lock.charAt(i);
                        if (ch == '1') {
                            descLock += (i + 1) + "号锁被打开 ";
                        } else {
                            descLock += (i + 1) + "号锁关闭 ";
                        }
                    }
                    tempMap.put("LOCKDESC", descLock);

                    if (mTemp != null) {
                        tempMap.put("EMP_IDNAME", mTemp.getSNAME());
                    }
                }
            }
        }
        return deviceMap;
    }

    @Override
    public CenterPointAndZoom getCPZ(Map<String, Object> map) {
        List<Map<String, Object>> list= dmapper.getDeviceByEmpId(map);
        List<GeoCoordinate> geoCoordinateList = new ArrayList<GeoCoordinate>();
        CenterPointAndZoom cp=new CenterPointAndZoom();


        for (Map<String, Object> item:
                list) {
            GeoCoordinate g = new GeoCoordinate(Common.toDouble(item.get("LATITUDE")), Common.toDouble(item.get("LONGITUDE")));
            geoCoordinateList.add(g);
        }
        cp= CenterPoint.GetCenterPointAndZoom(geoCoordinateList);

        return cp;
    }

    @Override
    public List<Map<String, Object>> getDeviceByEmpSetId(int empId) {
        return dmapper.getDeviceByEmpSetId(empId);
    }


    @Override
    public Map<String, Object> initList(int appId) {
        Map<String, Object> map = new HashMap<>();
        List<MTreeNode> deptList = dTreeMapper.getDept(appId);
        map.put("dept", deptList);
        return map;
    }

    @Override
    public Map<String, Object> initEdit(int appId) {
        List<MTreeNode> deptList = dTreeMapper.getDept(appId);
        Map<String, Object> map = new HashMap<>();
        List<MRef> post=dRefMapper.getPost(appId);
        map.put("post", post);
        map.put("dept", deptList);
        return map;
    }

    @Override
    public MTdevice getUserByImei(int empId, String imei) {
        return dmapper.getDeviceByImei(empId, imei);
    }

    @Override
    public List<MTusers> getUserByImei(String imei) {
        return dmapper.getUserByImei(imei);
    }

    @Override
    public boolean isOpen(int empId, int devId, String type) throws Exception {

        MTline_emp_set entity = dmapper.getLineSet(empId, devId);
        boolean flag = false;
        if (entity != null) {
            if (type.equals("c")  && entity.getCODEOPEN() == 1) {
                flag = true;
            } else if (type.equals("b")&& entity.getBLUETOOTHOPEN() == 1) {

                flag = true;
            } else if (type.equals("r") && entity.getREMOTEOPEN() == 1) {
                flag = true;
            }
        }

        return flag;
    }

    @Override
    public int saveData(MTline_emp_set mTline_emp_set, int[] ids) throws Exception {

        if (mTline_emp_set.getLINE_EMP_SET_ID() > 0) {
            this.update(mTline_emp_set);
        } else {
            mTline_emp_set.setBDEL(1);
            this.insert(mTline_emp_set);
        }
        if (ids.length > 0) {
            int empId = mTline_emp_set.getEMP_ID();
            List<MTdevice_emp_set> list = devicemapper.fetch("AND EMP_ID=" + empId);

            for (MTdevice_emp_set model : list
            ) {
                devicemapper.deleteById(model.getDEVICE_EMP_SET_ID());
            }
            for (int id : ids
            ) {
                MTdevice_emp_set model = new MTdevice_emp_set();
                model.setDEVICE_ID(id);
                model.setEMP_ID(empId);
                devicemapper.insert(model);
            }

        }
        return mTline_emp_set.getLINE_EMP_SET_ID();

    }

    @Override
    public Map<String, Object> getLineSetById(int id) {
        return dmapper.getLineSetById(id);
    }


}
